package com.tuju.jetpackfirstdemo.ui.flexbox

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.google.android.flexbox.*
import com.tuju.jetpackfirstdemo.R
import com.tuju.jetpackfirstdemo.adapter.FlexAdapter
import kotlinx.android.synthetic.main.activity_flex_box.*

class FlexBoxActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_flex_box)
        initView()
    }

    private fun initView() {
        val staggeredLayoutManager = StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
        val flexboxLayoutManager = FlexboxLayoutManager(this)
        flexboxLayoutManager.flexWrap = FlexWrap.WRAP
        flexboxLayoutManager.flexDirection = FlexDirection.ROW
        flexboxLayoutManager.justifyContent = JustifyContent.FLEX_START
        flexboxLayoutManager.alignItems = AlignItems.FLEX_START
//        flexboxLayoutManager.alignContent = AlignContent.FLEX_START
//Caused by: java.lang.UnsupportedOperationException: Setting the alignContent in the FlexboxLayoutManager is not supported. Use FlexboxLayout if you need to use this attribute.

        val adapter = FlexAdapter()
        initData(adapter)
        rv_flex.layoutManager = flexboxLayoutManager
        rv_flex.adapter = adapter

//        rv_flex.addOnScrollListener(object : RecyclerView.OnScrollListener() {
//            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
//                super.onScrolled(recyclerView, dx, dy)
//                //获取可见item个数
//                val lastVisibleItemPosition = (recyclerView.layoutManager as GridLayoutManager).findLastVisibleItemPosition()
//                //说明是最后一条数据
//                if (lastVisibleItemPosition + 1 == adapter.itemCount) {
//                    Snackbar.make(recyclerView, "一次只加载20条，查看更多请刷新哦。",
//                            Snackbar.LENGTH_SHORT).show()
//            }
//        })
    }

    private fun initData(adapter: FlexAdapter) {
        val lists = arrayListOf<String>("慕课网","极客学院","网易云课程","腾讯课堂","二维码","视频转gif,gif优化等","在线翻译","麦子","人工智能")
        adapter.addAll(lists)
    }
}
